package com.stox.ui.javafx.list.widget.menu;

import java.util.List;

import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import lombok.Delegate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractModel;
import com.stox.dto.WatchList;
import com.stox.dto.intf.AbstractModelNavigable;
import com.stox.ui.javafx.core.model.TreeViewAbstractModelSearchable;
import com.stox.ui.javafx.list.widget.menu.item.AddWatchListMenuItem;
import com.stox.ui.javafx.list.widget.menu.item.ClearAllMenuItem;
import com.stox.ui.javafx.list.widget.menu.item.ClearEntriesMenuItem;
import com.stox.ui.javafx.list.widget.menu.item.ClearWatchListsMenuItem;
import com.stox.ui.javafx.list.widget.menu.item.DeleteWatchListOrEntryMenuItem;
import com.stox.ui.javafx.list.widget.menu.item.RenameWatchListMenuItem;

@Component
public class WatchListContextMenu extends ContextMenu implements AbstractModelNavigable<List<AbstractModel>>{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	private RenameWatchListMenuItem renameMenuItem;
	
	@Autowired
	private ClearWatchListsMenuItem clearWatchListsMenuItem;
	
	@Autowired
	private ClearEntriesMenuItem clearEntriesMenuItem;
	
	@Autowired
	private ClearAllMenuItem clearAllMenuItem;
	
	@Autowired
	private AddWatchListMenuItem addWatchListMenuItem;
	
	@Autowired
	private DeleteWatchListOrEntryMenuItem deleteMenuItem;
	
	@Delegate
	private final TreeViewAbstractModelSearchable treeViewabstractModelSearchable = new TreeViewAbstractModelSearchable();
	
	private SeparatorMenuItem separator1 = new SeparatorMenuItem();
	private SeparatorMenuItem separator2 = new SeparatorMenuItem();
	
	@Override
	@SuppressWarnings("unchecked")
	public void show(Node node, double arg1, double arg2) {
		if(!(node instanceof TreeView)) return;
		TreeView<AbstractModel> treeView = (TreeView<AbstractModel>)node;
		treeViewabstractModelSearchable.setTreeView(treeView);
		getItems().removeAll(renameMenuItem, addWatchListMenuItem, separator1, clearWatchListsMenuItem, clearEntriesMenuItem, clearAllMenuItem, separator2, deleteMenuItem);
		List<TreeItem<AbstractModel>> treeItems = treeView.getSelectionModel().getSelectedItems();
		boolean selectionHasWatchList = false;
		for(TreeItem<AbstractModel> treeItem : treeItems){
			if(null != treeItem && null != treeItem.getValue()){
				if(treeItem.getValue() instanceof WatchList){
					selectionHasWatchList = true;
				}
			}
		}
		if(selectionHasWatchList){
			getItems().addAll(renameMenuItem,  
					addWatchListMenuItem, 
					separator1, 
					clearWatchListsMenuItem, 
					clearEntriesMenuItem, 
					clearAllMenuItem,
					separator2,
					deleteMenuItem);
		}else{
			getItems().add(deleteMenuItem);
		}
		super.show(treeView, arg1, arg2);
	}
	
}
